/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.gui.wizard.impl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.swing.SwingWorker;

import edu.drexel.sd0910.ece01.aqmon.data.ActiveNode;
import edu.drexel.sd0910.ece01.aqmon.gui.MainModel;
import edu.drexel.sd0910.ece01.aqmon.gui.wizard.WizardPanelDescriptor;

/**
 * Wizard Descriptor - Map panel. This is the fourth panel shown during the
 * setup wizard. See
 * {@link edu.drexel.sd0910.ece01.aqmon.gui.wizard.impl.MapPanel MapPanel}.
 * 
 * @author Kyle O'Connor
 * 
 */
public class MapPanelDescriptor extends WizardPanelDescriptor implements ActionListener {

	public static final String IDENTIFIER = "MAP_PANEL";

	private MapPanel panel;

	private String currentCard;

	public MapPanelDescriptor(MainModel model) {
		panel = new MapPanel(model);
		this.setPanelIdentifier(IDENTIFIER);
		this.setTargetPanel(panel);
		panel.addGoMapListener(this);
	}

	@Override
	public void aboutToDisplayPanel() {
		//switch to not map
		panel.switchCard(MapPanel.NO_MAP_CARD);
		currentCard = MapPanel.NO_MAP_CARD;
	}

	@Override
	public Object getBackPanelDescriptor() {
		return NodeDiscoveryPanelDescriptor.IDENTIFIER;
	}

	@Override
	public Object getNextPanelDescriptor() {
		return SummaryPanelDescriptor.IDENTIFIER;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if (currentCard.equals(MapPanel.NO_MAP_CARD)) {
			panel.switchCard(MapPanel.MAP_CARD);
			currentCard = MapPanel.MAP_CARD;
		}
		panel.moveMapLocation();
		panel.reset();
		loadNodesToDisplay();
	}

	private void loadNodesToDisplay() {
		new SwingWorker<Void, Void>() {

			@Override
			protected Void doInBackground() throws Exception {
					List<ActiveNode> list = panel.model.getXbeeManager().getActiveNodesList();
					synchronized (list) {
						Collections.sort(list, new Comparator<ActiveNode>() {
							public int compare(ActiveNode a1, ActiveNode a2) {
								return a1.getNodeID().compareTo(a2.getNodeID());
							}});
						Iterator<ActiveNode> i = list.iterator();
						while (i.hasNext()) {
							panel.setNodeVisible(i.next().getNodeID());
						}
					}
				return null;
			}
		}.execute();
	}

}
